package com.dmap.community.controller.app;

import com.dmap.base.vo.BaseVo;
import com.dmap.community.bo.reply.ReplyBo;
import com.dmap.community.bo.reply.ReplyCommentBo;
import com.dmap.community.constant.Constant;
import com.dmap.community.controller.app.param.ReplyCommentParam;
import com.dmap.community.controller.app.param.ReplyParam;
import com.dmap.community.service.community.ReplyService;
import com.dmap.community.vo.NormalResponseVo;
import com.dmap.community.vo.reply.ShowReplyCommentVo;
import com.dmap.community.vo.reply.ShowReplyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 14/11/17 Time: 14:25
 * \* Description:  社区回复
 * \
 */
@Api(value = "社区主题评论/主题评论回复", description = "App/社区主题评论/主题评论回复")
@RestController
@RequestMapping(value = "/api/dmap-community-server/v1/community/reply")
public class ReplyController {

    @Resource
    private ReplyService replyService;

    @ResponseBody
    @ApiOperation(value = "点赞社区主题评论(一级回复)", notes = "点赞社区主题评论(一级回复)")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseVo insertReply(@RequestBody ReplyParam param) {
        BaseVo result = new BaseVo();
        if (param.getThemeId() == 0 || param.getUserId() == 0 || StringUtils.isEmpty(param.getReplyComment())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = replyService.insertReplyTheme(param.getThemeId(), param.getUserId(), param.getReplyComment());
        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "点赞社区主题评论回复(二级回复)", notes = "点赞社区主题评论回复(二级回复)")
    @RequestMapping(value = "/comment/insert", method = RequestMethod.POST)
    public BaseVo insertReplyComment(@RequestBody ReplyCommentParam param) {
        BaseVo result = new BaseVo();
        if (param.getReplyId() == 0 || param.getUserId() == 0 || StringUtils.isEmpty(param.getReplyComment())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = replyService.insetReplyComment(param.getReplyId(), param.getUserId(), param.getReplyComment());
        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "删除社区主题评论(一级评论)", notes = "删除社区主题评论(一级评论)")
    @RequestMapping(value = "/delete/{replyId}", method = RequestMethod.DELETE)
    public BaseVo deleteReply(@PathVariable(value = "replyId") long replyId) {
        BaseVo result = new BaseVo();
        if (replyId == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = replyService.deleteReplyTheme(replyId);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "删除社区主题评论回复(二级评论)", notes = "删除社区主题评论回复(二级评论)")
    @RequestMapping(value = "/comment/delete/{commentId}", method = RequestMethod.DELETE)
    public BaseVo deleteReplyComment(@PathVariable(value = "commentId") long commentId) {
        BaseVo result = new BaseVo();
        if (commentId == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = replyService.deleteReplyComment(commentId);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
        }
        return result;
    }


    @ResponseBody
    @ApiOperation(value = "查询社区主题评论(一级回复)", notes = "查询社区主题评论(一级回复)")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public ShowReplyVo selectReply(@RequestParam long themeId, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        ShowReplyVo result = new ShowReplyVo();
        if (themeId == 0 || pageIndex == null || pageSize == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        List<ReplyBo> replyBoList = replyService.selectReplyTheme(themeId, pageIndex, pageSize);
        result.setData(replyBoList);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查询社区主题评论回复(二级回复)", notes = "查询社区主题评论回复(二级回复)")
    @RequestMapping(value = "/comment/select", method = RequestMethod.GET)
    public ShowReplyCommentVo selectReplyComment(@RequestParam long replyId, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        ShowReplyCommentVo result = new ShowReplyCommentVo();
        if (replyId == 0 || pageIndex == null || pageSize == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        List<ReplyCommentBo> replyCommentBoList = replyService.selectReplyComment(replyId, pageIndex, pageSize);
        result.setData(replyCommentBoList);
        return result;
    }


}
